package api

import (
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"net/http"
)

func CreateQuestion(c *gin.Context) {
	var req request.CreateQuestionReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": nil,
		})
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	question, err := c1.CreateQuestion(c, &__.CreateQuestionRequest{
		ExType:         req.ExType,
		MajorOne:       req.MajorOne,
		MajorTwo:       req.MajorTwo,
		Difficulty:     int64(req.Difficulty),
		Author:         req.Author,
		Founder:        req.Founder,
		FounderNum:     int64(req.FounderNum),
		Cretime:        req.Cretime,
		Reviewer:       req.Reviewer,
		ReviewerType:   int64(req.ReviewerType),
		ReviewerNumber: req.ReviewerNumber,
		ExName:         req.ExName,
		Option:         int64(req.Option),
		Answer:         int64(req.Answer),
		Keysword:       req.Keysword,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": question,
	})
}
func InfoQuestion(c *gin.Context) {
	var req request.InfoQuestionReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": nil,
		})
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	question, err := c1.InfoQuestion(c, &__.InfoQuestionRequest{
		QuestionsId: int64(req.QuestionsId),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": question,
	})
}
func FindQuestion(c *gin.Context) {

}
func DelQuestion(c *gin.Context) {

}
func UpdateQuestion(c *gin.Context) {

}
